package com.kaifamiao.month03.day0310;

import java.util.Arrays;

public class DeleteData {
    public static void main(String[] args) {
        /*
        * 两种方法实现
        * int[] scores = {90, 74, 70, 100, 59, 80, 85};
        * 1、将 成绩数组的长度变为 5
        * 2、把成绩数组中 100 删除掉
        * */
        int[] scores = {90, 74, 70, 100, 59, 80, 85};
        changeArrayLengthFirst(scores);
        changeArrayLengthSecond(scores);
        deleteArrayDataFirst(scores,100);
        deleteArrayDataSecond(scores,100);
    }

    public static void changeArrayLengthFirst(int[] array){
        int[] newArray = Arrays.copyOf(array,5);
        printArray(newArray);
    }

    public static void changeArrayLengthSecond(int[] array){
        int[] newArray = new int[5];
        for (int i = 0; i < newArray.length; i++) {
            newArray[i] = array[i];
        }
        printArray(newArray);
    }

    public static void deleteArrayDataFirst(int[] array, int key){
        printArray(deleteElement1(array,Arrays.binarySearch(array,key)));
    }

    public static int[] deleteElement1(int[] arrays, int index){
        int length = arrays.length;
        //判断数据合理性
        if (index >= 0 && index < length) {
            int[] arrays_result = new int[arrays.length - 1];
            //将arrays数组在index前的元素都复制到新数组arrays_result中
            System.arraycopy(arrays, 0, arrays_result, 0, index);
            //判断index之后是否还有元素，有则将index后的元素从index位置复制到新数组中
            if (index < length - 1) {
                System.arraycopy(arrays, index + 1, arrays_result, index, arrays_result.length - index);
            }
            return arrays_result;
        } else {
            //不合理，抛越界异常
            throw new IndexOutOfBoundsException("index :" + index + ", length: " + length);
        }
    }

    public static void deleteArrayDataSecond(int[] array, int key){
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key){
                index = i;
            }
        }
        printArray(deleteElement2(array,index));
    }
    public static int[] deleteElement2(int[] arrays, int index){
        int length = arrays.length;
        //判断数据合理性
        if (index >= 0 && index < length) {
            int[] arrays_result = new int[arrays.length - 1];
            //将arrays数组在index前的元素都复制到新数组arrays_result中
            arraycopy(arrays, 0, arrays_result, 0, index);
            //判断index之后是否还有元素，有则将index后的元素从index位置复制到新数组中
            if (index < length - 1) {
                arraycopy(arrays, index + 1, arrays_result, index, arrays_result.length - index);
            }
            return arrays_result;
        } else {
            //不合理，抛越界异常
            throw new IndexOutOfBoundsException("index :" + index + ", length: " + length);
        }
    }
    public static void arraycopy(int[] src, int from, int[] dst, int to, int L) {
        for (int i = 0; i < L; i++) {
            dst[to + i] = src[from + i];
        }
    }

    public static void  printArray(int[] array){
        String str = "";
        for (int i = 0; i < array.length; i++) {
            if(i == 0){
                str += "[" + array[i];
            }else if(i == array.length-1){
                str += ", " + array[i] + "]";
            }else{
                str += ", " + array[i];
            }
        }
        System.out.println(str);
    }
}
